package com.club.football.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.club.football.dto.PageDto;
import com.club.football.dto.PlayerSaveDto;
import com.club.football.entity.Coach;
import com.club.football.entity.Player;
import com.club.football.req.PlayerPageReq;
import com.club.football.service.PlayerService;
import com.club.football.utils.FileUtils;
import com.club.football.utils.R;
import com.club.football.vo.PlayerPageVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.club.football.common.CommonConstants.DEL_OK_MSG;
import static com.club.football.common.CommonConstants.GET_INFO_OK;

/**
 * 球员Controller
 *
 * @author 1
 * @date 2023/12/28
 */
@RestController
@RequestMapping("/player")
public class PlayerController {

    @Resource
    private PlayerService playerService;


    @GetMapping("/page")
    public R<Page<PlayerPageVo>> page(PageDto<Player> pageDto, PlayerPageReq playerPageReq) {
        return playerService.playerPage(pageDto, playerPageReq);
    }

    /**
     * 球队保存
     *
     * @return {@link R}<{@link String}>
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody @Valid PlayerSaveDto saveDto) {
        return playerService.playerSave(saveDto);
    }

    /**
     * 删除
     *
     * @param id ID
     * @return {@link R}<{@link String}>
     */
    @DeleteMapping("/remove/{id}")
    public R<String> remove(@PathVariable Long id) {
        playerService.removeById(id);
        return R.ok(DEL_OK_MSG);
    }

    /**
     * 信息
     *
     * @param id ID
     * @return {@link R}<{@link Coach}>
     */
    @GetMapping("/info/{id}")
    public R<Player> info(@PathVariable Long id) {
        Player player = playerService.getById(id);
        player.setPlayerImg(FileUtils.getUrl(player.getPlayerImg()));
        return R.ok(player, GET_INFO_OK);
    }
}
